package tronglv.gs.view;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.Entity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;

import tronglv.gs.control.SceneManager;
import tronglv.gs.model.BaseScene;
import tronglv.gs.model.Share;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;

public class menuScene extends BaseScene {
	private int w, h;
	private PhysicsWorld mPhysicsWorld;
	private Sprite spriArcade,sMusic,sSound;
	private Entity etButton;

	@Override
	public void createScene() {
		w = Share.width;
		h = Share.height;
		camera.setCenter(w / 2, h / 2);
		etButton = new Entity();
		draw();
		engine.registerUpdateHandler(new FPSLogger());
		this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0),
				false, 8, 1);
		this.registerUpdateHandler(this.mPhysicsWorld);
	}

	private void draw() {
		// initialize and attach
		attachChild(new Sprite(0, 0, resourcesManager.menu_bg_region, vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
		spriArcade = new Sprite(w / 2
				- resourcesManager.menu_btnplay_region.getWidth() / 2, h / 3
				+ resourcesManager.menu_btnplay_region.getHeight() + 5,
				resourcesManager.menu_btnplay_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				boolean istouch = true;
				switch (pSceneTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					this.setScale(1.05f);
					istouch = true;
					break;
				case TouchEvent.ACTION_UP:
					if (istouch) {
						istouch = false;
						this.setScale(1.0f);
						try {
							SceneManager.getInstance().createMenutoPlay();
						} catch (Exception e) {
							activity.runOnUiThread(new Runnable() {
								public void run() {
									Toast.makeText(activity,
											"Thao tác bị lỗi vui lòng thử lại",
											Toast.LENGTH_SHORT).show();
								}
							});
						}
					}
					break;
				}
				return true;
			}
		};
		this.registerTouchArea(spriArcade);
		etButton.attachChild(spriArcade);
		this.attachChild(etButton);
	}

	@Override
	public void onBackKeyPressed() {
		System.exit(0);
	}

}
